package com.xiaobao.coalproperty.other.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.xiaobao.coalproperty.R;

import java.util.ArrayList;
import java.util.List;

public class ChartView extends View {

    private float x, y;
    private float padding;
    private float xAvg, yAvg;
    private float mWidth, mHeight;

    private Paint pointPaint;
    private Paint linePaint;
    private Paint pathPaint;
    private Paint textPaint;

    private PointF endX, endY;

    private List<String> xData = new ArrayList<>();
    private List<Float> yData = new ArrayList<>();

    private Context mContext;

    private static final String TAG = "ChartView";

    public ChartView(Context context) {
        super(context);
    }

    public ChartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    public void setData(List<String> xData, List<Float> yData) {
        this.xData = xData;
        this.yData = yData;
        this.invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.mWidth = w;
        this.mHeight = h;
        this.x = mWidth - padding;
        this.y = mHeight - padding;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initPaint();
        Log.e(TAG, "onDraw: " );
//        canvas.drawColor(Color.rgb(30, 180, 134));//背景色
        drawChart(canvas);
        drawText(canvas);
    }

    private void drawChart(Canvas canvas) {
        Path path = new Path();//阴影效果
        canvas.save();
        canvas.translate((float) (padding * 1.5), y);//将坐标原点挪到左下角
        path.moveTo(0, 0);
        endX = new PointF(x - padding, 0);
        endY = new PointF(0, -y + padding);
//        canvas.drawLine(0, 0, endX.x, endX.y, linePaint);
//        canvas.drawLine(0, 0, endY.x, endY.y, linePaint);

        xAvg = endX.x / (xData.size() - 1);//x轴的平均值
        yAvg = endY.y / getMax(yData);//y轴的平均值

        for (int i = 0; i < yData.size(); i++) {
            if (i == 0) {
                path.quadTo(0, 0, xAvg * i, yAvg * yData.get(i));
            }
            canvas.drawCircle(xAvg * i, yAvg * yData.get(i), 10, pointPaint);//画点
            if (i != yData.size() - 1) {
                canvas.drawLine(xAvg * i, yAvg * yData.get(i), xAvg * (i + 1), yAvg * yData.get(i + 1), linePaint);//画线
                path.quadTo(xAvg * i, yAvg * yData.get(i), xAvg * (i + 1), yAvg * yData.get(i + 1));
            } else {
                path.quadTo(xAvg * i, yAvg * yData.get(i), endX.x, 0);
            }
        }
        path.close();
        canvas.drawPath(path, pathPaint);//画阴影
        canvas.restore();
    }

    private void drawText(Canvas canvas) {
        canvas.save();
        canvas.translate((float) (padding * 1.5), y);//将坐标原点挪到左下角
        for (int i = 0; i < yData.size(); i++) {
            canvas.drawText(xData.get(i), xAvg * i - padding / 2, padding / 2, textPaint);
            // TODO: 2018/9/26 改成Y数据
//            canvas.drawText(yData.get(i) + "", (float) (-padding * 1.2), yAvg * yData.get(i), textPaint);
        }
        canvas.restore();
    }

    private void initPaint() {

        pointPaint = new Paint();
        pointPaint.setColor(Color.WHITE);
        pointPaint.setStrokeWidth(50);
        pointPaint.setAntiAlias(true);
        pointPaint.setStyle(Paint.Style.FILL);

        linePaint = new Paint();
        linePaint.setColor(Color.WHITE);
        linePaint.setStrokeWidth(5);
        linePaint.setAntiAlias(true);
        linePaint.setStyle(Paint.Style.STROKE);

        pathPaint = new Paint();
        pathPaint.setColor(Color.argb(30, 220, 220, 220));
        pathPaint.setStrokeWidth(5);
        pathPaint.setAntiAlias(true);
        pathPaint.setStyle(Paint.Style.FILL);

        textPaint = new Paint();
        textPaint.setColor(Color.WHITE);
        textPaint.setStrokeWidth(2);
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(mContext.getResources().getDimension(R.dimen._12px_sp));
        textPaint.setStyle(Paint.Style.STROKE);

        padding = textPaint.measureText("俩字");
    }

    private float getMax(List<Float> yData) {
        float max = 0;
        for (Float f : yData) {
            if (f > max) {
                max = f;
            }
        }
        return max;
    }
}
